from flask import Flask
from app.config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_wtf.csrf import CSRFProtect
from flask_debugtoolbar import DebugToolbarExtension


db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = "backend.login"
csrf = CSRFProtect()
toolbar = DebugToolbarExtension()


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)

    login.init_app(app)
    csrf.init_app(app)
    toolbar.init_app(app)

    from app.backend import bp as backendbp
    app.register_blueprint(backendbp, url_prefix='/backend')
    from app.frontend import bp as frontendbp
    app.register_blueprint(frontendbp, url_prefix='/')

    return app
